#include "curveshowwidget.h"
#include "plotcurveviewitem.h"
#include <QDebug>

CurveShowWidget::CurveShowWidget(PlotData *data, QWidget *parent)
	: QListWidget(parent),
	m_plotData(data)
{
	initCurves();
}

CurveShowWidget::~CurveShowWidget()
{

}

void CurveShowWidget::initCurves()
{
	int curveCount = m_plotData->getCurveCount();
	for (int i = 0; i < curveCount; i ++)
	{
		this->addCurve(m_plotData->getCurveName(i));
	}
}

void CurveShowWidget::addCurve(std::string cur_name)
{
	PlotCurveViewItem *item = new PlotCurveViewItem(cur_name);
	QObject::connect(item, SIGNAL(checkStateChanged(std::string, bool)), this, SIGNAL(checkStateChanged(std::string, bool)));
	QListWidgetItem *listItem = new QListWidgetItem(this);
	listItem->setSizeHint(QSize(35, 35));
	this->setItemWidget(listItem, item);
}

void CurveShowWidget::update()
{
	for (int i = 0; i < this->count(); i ++)
	{
		PlotCurveViewItem *item = (PlotCurveViewItem *)(this->itemWidget(this->item(i)));
		int curId = m_plotData->getCurveId(item->getCurveName());
		if(curId < 0) return;
		double **ydata = m_plotData->getYData();
		int pos = m_plotData->getCurPos(curId);
		item->SetLastValue(ydata[curId][pos]);
	}
}

// slots
void CurveShowWidget::SetLastValue(std::string cur_name, double value)
{
	for (int i = 0; i < this->count(); i ++)
	{
		PlotCurveViewItem *item = (PlotCurveViewItem *)(this->itemWidget(this->item(i)));
		if (item->getCurveName() == cur_name)
		{
			item->SetLastValue(value);
			break;
		}
	}
}
